<?

	class FTAGallery extends FTAObject {
		
		var $cmsDocument;//cms document needs a tart up.. to be nicer cache friendly..
		var $galleryImages;
		var $gallery;
		
		var $imageElementId;
		var $enlargementFileType;
		var $thumnailFileType;
		var $thumnailElementId;
		
		// var $hasInsertedLightBoxJS;
		
		function FTAGallery() {
			parent::FTAObject();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			//set up the base options on the cms document.. so we only need to do it once??
			global $FTAGalleryImageElementId;			
			$this->imageElementId = (0 < $FTAGalleryImageElementId) ? $FTAGalleryImageElementId : 11362;
			
			global $FTAGalleryThumbnailImageElementId;			
			$this->thumnailElementId = (0 < $FTAGalleryThumbnailImageElementId) ? $FTAGalleryThumbnailImageElementId : 20079;
			
			global $FTAGalleryEnlargementFileType;
			$this->enlargementFileType = (0 < $FTAGalleryEnlargementFileType) ? $FTAGalleryEnlargementFileType : 4;
			
			global $FTAGalleryThumbnailFileType;
			$this->thumnailFileType = (0 < $FTAGalleryThumbnailFileType) ? $FTAGalleryThumbnailFileType : 5;
			
			// $this->hasInsertedLightBoxJS = false;
		
		}
		
		function openGallery($galleryIdentifier){
	
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docFileMod');
			$this->cmsDocument->addConstraint('author',1);

			if (is_numeric( ($galleryIdentifier * 1) ) &&  0 < ($galleryIdentifier * 1)){
				$this->cmsDocument->addConstraint('documentcode',$galleryIdentifier);	
			} else {
				$this->cmsDocument->addConstraint('title',$galleryIdentifier);
			}
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				$this->gallery = $this->cmsDocument->nextDocumentFromCache();

				//echo "<!-- gallery opened -->";
				return true;
			} else {
				$this->gallery = array();				
				return false;
			}
		}
		
		
		function openGalleryImages() {
			
			if  (!$this->gallery)
				return false;
				
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docFileMod');
			$this->cmsDocument->addConstraint('author',0);			
			$this->cmsDocument->addConstraint('categorycode',$this->gallery->documentcode);
			$this->cmsDocument->addSort('<<rank>>');
			
			$this->cmsDocument->setPageSize(null);
			
			$this->cmsDocument->resetIterator();
			
			//mosso does not like this at all :(
			//$this->galleryImages = $this->cmsDocument->getAllDocuments();
			$this->galleryImages = array();
			while ($row  = $this->cmsDocument->nextDocumentFromCache()){
				array_push($this->galleryImages, $row);
			}
			
			if (!$this->galleryImages) {
				$this->galleryImages = array();
				return false;
			}

			return true;
		}
		
		
		
		
		
		
		function renderImages(){			
?>
<ul class="thumbs">
<?
			foreach($this->galleryImages as $image){

				$imageThumbNailUrl = "/html/blob.php?documentCode=" . $image->documentcode . "&elementId=". $this->thumnailElementId . "&filetypecode=" . $this->thumnailFileType;
				// $imageEnlargementUrl = "/html/blob.php?documentCode=" . $image->documentcode . "&elementId=". $this->imageElementId . "&filetypecode=" . $this->enlargementFileType;
				$galleryDoc = $this->cmsDocument->openDocumentFromCache($this->gallery->categorycode);
				
				$imageURL = "/" . urlFragmentClean($galleryDoc->title) . "/" . urlFragmentClean($this->gallery->title) . "/" . urlFragmentClean($image->title) . "/#" . $image->documentcode;
				
				
				
?>
		<li><a href="<?= $imageURL ?>"><img id="displayImage" src="<?= $imageThumbNailUrl ?>"  alt="<?= $image->title ?>" border="0" /></a></li>
<?
			}
?>
</ul>
<?					
/*		if (!$this->hasInsertedLightBoxJS) {
				$this->hasInsertedLightBoxJS = true;
?>
<script type="text/javascript" src="/js/lightbox.js"></script>
<?				
		}
*/					
		}		
	}
	
	
////////


	function fta_gallery($galleryIdentifier) {		
		ob_start();
		$ftaGallery = &fta_getSharedGalleryObject();		
		if ( $ftaGallery->openGallery($galleryIdentifier) && $ftaGallery->openGalleryImages()){
			$ftaGallery->renderImages();
		}
		return ob_get_clean();
	}
	
	function &fta_getSharedGalleryObject(){

		global $fta_;

		global $FTAGalleryClass;
		
		if(!is_null($FTAGalleryClass) && "FTAGallery" != $FTAGalleryClass){
		} else {
			$FTAGalleryClass = "FTAGallery";
		}
			
		if (!array_key_exists('ftaGallery',$fta_)){
			$fta_->ftaGallery = new  $FTAGalleryClass();
		}
		return $fta_->ftaGallery;
	}